<template>
  <!-- 新增和编辑目录弹框 -->
  <el-dialog
  :title="isEdit ? '修改目录' :'新增目录'"
  :visible.sync="dialogVisible"
  @close="handleClose"
  width="30%"
 >
  <span>
    <el-form ref="form" :model="formData" :rules="rules"  label-width="80px">
      <el-form-item label="所属学科" prop="subjectID">
        <el-select v-model="formData.subjectID" placeholder="请选择" style="width:100%">
          <el-option v-for="item in subjectList" :label="item.label" :value="item.value" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="目录名称" prop="directoryName">
        <el-input v-model="formData.directoryName"></el-input>
      </el-form-item>
      </el-form>
  </span>
  <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="submit">确 定</el-button>
  </span>
</el-dialog>
</template>

<script>
import { simple as subjectSimple } from '@/api/hmmm/subjects'
import { add as directoryAdd, detail as directoryDetail, update as directoryUpdate } from '@/api/hmmm/directorys'

export default {
  props: {
    isEdit: {
      type: Boolean,
      required: true
    },
    getDirectoryList: {
      type: Function,
      required: true
    },
    id: {
      type: Number,
      required: true
    },
    getSubjectdirectory: {
      type: Function,
      required: true
    },
    subjectID: {
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      formData: {
        subjectID: '', // 所属学科Id
        directoryName: '' // 目录名称
      },
      dialogVisible: false, // 弹框是否显示
      subjectList: [], // 简单学科列表
      // 表单校验
      rules: {
        subjectID: [{ required: true, message: '请选择所属学科', trigger: 'blur' }],
        directoryName: [{ required: true, message: '请填写目录名称', trigger: 'blur' }]
      }
    }
  },
  created () {
    // 获取简单学科列表
    this.getSubjectList()
  },
  methods: {
    // 弹层显示
    dialogFormV () {
      this.dialogVisible = true
    },
    // 弹层隐藏
    dialogFormH () {
      this.dialogVisible = false
    },
    // 弹层关闭触发
    handleClose () {
      // 清空输入框
      this.formData.subjectID = ''
      this.formData.directoryName = ''
      // 重置表单(包括校验规则)
      this.$refs.form.resetFields()
    },
    // 新增目录
    async addDirectory () {
      // 调用接口，发送请求
      const res = await directoryAdd({ subjectID: this.formData.subjectID, directoryName: this.formData.directoryName })
      // console.log(res)
      if (res.status === 200) this.$message.success('新增成功')
    },
    // 获取目录详情
    async getDirectoryDetail (id) {
      // console.log(id)
      // 调用接口发送请求
      const { data } = await directoryDetail({ id })
      // console.log(data)
      // 服务器返回的数据传给data
      this.formData.subjectID = data.subjectID
      this.formData.directoryName = data.directoryName
    },
    // 编辑目录
    async editDirectory () {
      // 调用接口 发送请求
      const { data } = await directoryUpdate({ id: this.id, subjectID: this.formData.subjectID, directoryName: this.formData.directoryName })
      // console.log(data)
      if (data.success) this.$message.success('编辑成功')
    },
    // 获取简单学科列表
    async getSubjectList () {
      // 发送请求 获取简单学科数据列表
      const { data } = await subjectSimple()
      // console.log(data)
      // 将服务器返回的数据传给学科列表
      this.subjectList = data
    },
    // 弹框确定按钮
    submit () {
      // 兜底校验
      this.$refs.form.validate(valid => {
        if (valid) {
          // 新增学科或编辑学科
          this.isEdit ? this.editDirectory() : this.addDirectory()
          // 如果是编辑操作并且学科传过来学科ID
          if (this.isEdit === true && this.subjectID !== '') {
            // 获取当前ID的目录列表
            this.getSubjectdirectory(this.subjectID)
          } else {
            // 获取目录列表  调用父组件的方法
            this.getDirectoryList()
          }
          // 弹层隐藏
          this.dialogFormH()
        }
      })
    }
  }
}
</script>

<style scoped lang='less'></style>
